﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TaskHub.wsAccount;
using TaskHub.LocalStorage;
namespace TaskHub
{
    public partial class EditRegister : PhoneApplicationPage
    {
        private int iCurrentUser;
        private ucProgress _Progress;
        private ucProgress Progress
        {
            get
            {
                if (_Progress == null)
                {
                    _Progress = new ucProgress();
                }
                return _Progress;
            }
        }

        public EditRegister()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                uint uiOwner;
                Progress.Show();
                uint.TryParse ( IsolatedStorageDAO.GetMyId().ToString(), out uiOwner );
                TaskHub.wsAccount.AccountSoapClient ws = new TaskHub.wsAccount.AccountSoapClient();
                ws.ChangeAccountInformationCompleted += new EventHandler<ChangeAccountInformationCompletedEventArgs>(ws_ChangeAccountInformationCompleted);
                ws.ChangeAccountInformationAsync(uiOwner , this.txtEmail.Text.ToString(), this.txtPhone.Text.ToString(), this.txtName.Text.ToString(), this.txtPwd.Password.ToString());
            }
            catch (Exception)
            {
                throw;
            }
        }

        void ws_ChangeAccountInformationCompleted(object sender, ChangeAccountInformationCompletedEventArgs e)
        {
            Progress.Hide();
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message, "Error", MessageBoxButton.OK);
                return;
            }

            bool ok = e.Result;
            if (ok)
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml"));
            }
            else
            {
                MessageBox.Show("Error.");
            }
        }
       
        private void btnCancel_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                iCurrentUser = IsolatedStorageDAO.GetMyId();
                loadProfile(iCurrentUser);
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void loadProfile(int iCurrentUser)
        {
            try
            {
                uint uiCurrentUser;

                uint.TryParse(iCurrentUser.ToString(), out uiCurrentUser);
                wsAccount.AccountSoapClient ws = new AccountSoapClient();

                ws.GetAccountInformationCompleted += new EventHandler<GetAccountInformationCompletedEventArgs>(ws_AccountInformationCompleted);
                ws.GetAccountInformationAsync(uiCurrentUser);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        void ws_AccountInformationCompleted(object sender, wsAccount.GetAccountInformationCompletedEventArgs e) 
        {
                // Something bad happened!!! //
                if (e.Error != null && e.Result != null )
                {
                    AccountInformation accountInfo = new AccountInformation();

                    accountInfo = e.Result;

                    this.txtName.Text = accountInfo.Name;
                    this.txtEmail.Text = accountInfo.Email;
                    this.txtPhone.Text = accountInfo.TelephoneNumber;
                    this.txtPwd.Password = string.Empty;

                }

            }
        


        
    }
}